import java.util.ArrayList;
import java.util.List;

/*
 * @lc app=leetcode.cn id=228 lang=java
 *
 * [228] 汇总区间
 *
 * https://leetcode-cn.com/problems/summary-ranges/description/
 *
 * algorithms
 * Medium (49.80%)
 * Likes:    31
 * Dislikes: 0
 * Total Accepted:    5.7K
 * Total Submissions: 11.4K
 * Testcase Example:  '[0,1,2,4,5,7]'
 *
 * 给定一个无重复元素的有序整数数组，返回数组区间范围的汇总。
 *
 * 示例 1:
 *
 * 输入: [0,1,2,4,5,7]
 * 输出: ["0->2","4->5","7"]
 * 解释: 0,1,2 可组成一个连续的区间; 4,5 可组成一个连续的区间。
 *
 * 示例 2:
 *
 * 输入: [0,2,3,4,6,8,9]
 * 输出: ["0","2->4","6","8->9"]
 * 解释: 2,3,4 可组成一个连续的区间; 8,9 可组成一个连续的区间。
 *
 */

// @lc code=start
class Solution {
    public List<String> summaryRanges(int[] nums) {
        int length = nums.length;
        List<String> res = new ArrayList<>();
        if(length == 0) {
            return res;
        }
        StringBuilder sb = new StringBuilder(String.valueOf(nums[0]));

        for(int index=1; index < nums.length; index++) {
            if(nums[index] - nums[index-1] > 1) {
                if(!sb.toString().equals(String.valueOf(nums[index-1])))
                    sb.append("->" + nums[index-1]);
                res.add(sb.toString());
                sb = new StringBuilder(String.valueOf(nums[index]));
            }
            if(index == length - 1 && nums[index] - nums[index-1] == 1)
                 sb.append("->" + nums[index]);
        }

        res.add(sb.toString());
        return res;
    }
}
// @lc code=end

